package com.lbs.jiepiaisi.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.adapter.WeiZhangLineAdapter;
import com.lbs.jiepiaisi.base.BaseActivity;
import com.lbs.jiepiaisi.bean.EquipmentEntity;
import com.lbs.jiepiaisi.bean.WeiZhangEntity;
import com.lbs.jiepiaisi.biz.WeiZhangChaXunBiz;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.view.WaitingDialogControll;

/**
 *
 * 时间：2017/11/8 16:50
 * 作用：查询结果
 */
public class ChaXunJieGuoActivity extends BaseActivity {
    //城市代码
    private String chengshidaima;
    //车牌号码
    private String chepaihaoma;
    //号牌类型
    private String haopaileixing;
    //发动机号
    private String fadongjihao;
    //车架号码
    private String chejiahaoma;

    private WeiZhangResultReceiver weiZhangResultReceiver;


    private RecyclerView recyclerViewResult;

    private ImageView mNoData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isTemplate = true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chaxunjieguo);
        Intent intent = getIntent();
        //城市代码
        chengshidaima = intent.getStringExtra("city");
        //车牌号码
        chepaihaoma = intent.getStringExtra("hphm");
        //号牌类型
        haopaileixing = intent.getStringExtra("hpzl");
        //发动机号
        fadongjihao = intent.getStringExtra("engineno");
        //车架号码
        chejiahaoma=intent.getStringExtra("classno");
        weiZhangResultReceiver = new WeiZhangResultReceiver();
        getApplicationContext().registerReceiver(weiZhangResultReceiver, new IntentFilter(Constant.ACCOUNT_WEI_ZHANG));

//        Log.e("TAG","传过来的城市代码："+chengshidaima);
//
//        Log.e("TAG","传过来的车牌号码："+chepaihaoma);
//
//        Log.e("TAG","传过来的号牌类型："+haopaileixing);
//
//        Log.e("TAG","传过来的发动机号："+fadongjihao);
//
//        Log.e("TAG","传过来的车架号码："+chejiahaoma);
        findView();
        setListener();
        getExras();


    }

    public void getExras() {
        WeiZhangChaXunBiz weiZhangChaXunBiz=new WeiZhangChaXunBiz();
        if(chengshidaima == null || chepaihaoma == null ||haopaileixing == null ||fadongjihao == null ||chejiahaoma == null){
            noData();
            return;
        }
        weiZhangChaXunBiz.weiZhangChaXunBiz(chengshidaima,chepaihaoma,haopaileixing,fadongjihao,chejiahaoma);
    }

    public void findView() {
        recyclerViewResult = (RecyclerView)findViewById(R.id.recy_data);
        recyclerViewResult.setLayoutManager(new LinearLayoutManager(this));

        mNoData = (ImageView)findViewById(R.id.iv_no_data);

    }

    public void setListener() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getApplicationContext().unregisterReceiver(weiZhangResultReceiver);
    }

    private void noData(){
        titleView.setText(chepaihaoma);
        recyclerViewResult.setVisibility(View.GONE);
        Toast.makeText(ChaXunJieGuoActivity.this,"车辆信息错误",Toast.LENGTH_SHORT).show();
        mNoData.setVisibility(View.VISIBLE);
    }

    private void noDataFind(WeiZhangEntity weiZhangEntity){
        titleView.setText(chepaihaoma);
        recyclerViewResult.setVisibility(View.GONE);
        if(weiZhangEntity.getLists().size() == 0){
            Toast.makeText(ChaXunJieGuoActivity.this,"暂无违章信息",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(ChaXunJieGuoActivity.this,weiZhangEntity.getMessage(),Toast.LENGTH_SHORT).show();
        }
        mNoData.setVisibility(View.VISIBLE);
    }

    private void onFail(WeiZhangEntity weiZhangEntity){
        titleView.setText(chepaihaoma);
        recyclerViewResult.setVisibility(View.GONE);
        Toast.makeText(ChaXunJieGuoActivity.this,weiZhangEntity.getMessage(),Toast.LENGTH_SHORT).show();
        mNoData.setVisibility(View.VISIBLE);
    }



    class WeiZhangResultReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            WaitingDialogControll.dismissWaitingDialog();
            WeiZhangEntity weiZhangEntity = (WeiZhangEntity)intent.getSerializableExtra(Constant.KEY_DATA);
            if(weiZhangEntity.getStatus() == 1){
                if(weiZhangEntity.getLists() == null || weiZhangEntity.getLists().size()  < 1){
                    //noData();
                    noDataFind(weiZhangEntity);
                    return;

                }
                mNoData.setVisibility(View.GONE);
                titleView.setText(weiZhangEntity.getHphm());
                recyclerViewResult.setVisibility(View.VISIBLE);
                recyclerViewResult.setAdapter(new WeiZhangLineAdapter(ChaXunJieGuoActivity.this,weiZhangEntity.getLists()));
            }else {
                //noData();
                onFail(weiZhangEntity);
            }


        }
    }


}
